<?php

namespace App\V3\V3_2_7\Models;

use App\Api\Models\SalesReturnPackageModel;
use Illuminate\Database\Eloquent\Model;
use DateTimeInterface;

/**
 * 退货
 * Class SalesReturnModel
 * @package App\Models
 */
class SalesReturnModel extends Model
{


    protected $table = 'sales_return';

    protected $dateFormat = 'U';//这个时间字段被为被格式化为UNIX 时间戳的形式存储
    //toArray 时间转换问题
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
    /**
     * $dates 数组内的列转换为日期格式
     *
     * @var array
     */
    protected $dates = [
        'refund_date',
    ];

    public function returnMateriel()
    {
        return self::hasMany(SalesReturnGoodsDetailModel::class, 'return_id', 'id');
    }
    
    public function goodsDetailInfo()
    {
        return self::hasMany(SalesReturnGoodsDetailModel::class, 'return_id', 'id');
    }

    public function returnPackage()
    {
        return self::hasMany(SalesReturnPackageModel::class, 'return_id', 'id');
    }
    
    public function returnLogisticsInfo(){
        return self::hasMany(SalesReturnLogisticsInfoModel::class, 'return_id', 'id');
        
    }
   

    static public function getSalesIdInfo($userId, $data, $condition)
    {
        return self::select('id')
            ->from('sales_return as a')
            ->where($condition)
            ->where($userId)->get()->toArray();
    }
    //今日退货数量
    static public function getTodayReturnCount($condition, $startTime, $endTime)
    {
        return self::where($condition)
                ->where('delete_flag',0)
                ->where('repeal_status',0)
                ->whereBetween('created_at', [$startTime, $endTime])
                ->count();
    }
    
    /**
     * 查询退货信息
     * @param unknown $id
     */
    static public function getsalesReturnInfo($id)
    {
        return self::select(
            'a.*',
            'b.username',
            'b.nickname',
            'a.id as return_id',
            'c.nickname as approver_name'
            )->from('sales_return as a')
            ->leftJoin('admin as b', 'b.id', '=', 'a.user_id')
            ->leftJoin('admin as c', 'b.id', '=', 'a.approver_id')
            ->with('returnLogisticsInfo', function($query) {
                $query->select(
                    'id',
                    'return_id',
                    'logistics_company',
                    'logistics_order_no'
                    )->where('delete_flag',0);
            })
            ->where([
                'a.id' => $id,
                'a.delete_flag' => 0
            ])->first()->toArray();
    }
    
    /**
     *条件查询退货信息
     * @param unknown $userId
     * @param unknown $data
     * @return unknown
     */
    static public function getBarterReturnList($userId, $data)
    {
                $query = SELF::select('id')
                    ->where([
                        'delete_flag'=>0,
                ])->where($userId);
                if(!empty($data['sales_return_idArr'])){
                        $query->whereIn('id',$data['sales_return_idArr']);
                }
        return $query->where(function($query) use($data){
                    if($data['search_type'] == 3){
                        $query->where('order_no', 'like', '%'.$data['keyword'].'%');
                    }elseif ($data['search_type'] == 4){
                        $query->where('out_order_no', 'like', '%'.$data['keyword'].'%');
                    }elseif($data['search_type'] == 6){
                        $query->where('customer_name', 'like', '%'.$data['keyword'].'%');
                    }
                })->get()->toArray();
    }
}
